﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;
using System.Web;
using System.Web.UI.WebControls;

namespace AutocompleteDemo.Layouts
{
    public partial class ServiceManage : LayoutsPageBase
    {
        private AutoCompletionServiceApplication m_alertManagerApplication;

        internal AutoCompletionServiceApplication CurrentServiceApplication
        {
            get
            {
                if (null == this.m_alertManagerApplication)
                {
                    string strApplicationId = HttpContext.Current.Request.QueryString["appid"];
                    this.m_alertManagerApplication = GetAutoCompletionApplication(strApplicationId);
                }
                return this.m_alertManagerApplication;
            }
        }

        // Static helper Methods
        internal static AutoCompletionServiceApplication GetAutoCompletionApplication(string strApplicationId)
        {
            AutoCompletionServiceApplication application = null;
            AutoCompletionService service = GetAutoCompletionService();
            if ((null != service) && !string.IsNullOrEmpty(strApplicationId))
            {
                Guid guid = new Guid(strApplicationId);
                foreach (AutoCompletionServiceApplication application2 in service.Applications)
                {
                    if (guid == application2.Id)
                    {
                        application = application2;
                        break;
                    }
                }
            }
            if (application == null)
            {
                application = AutoCompletionServiceApplicationProxy.GetProxy(SPServiceContext.Current).AutoCompletionApplication;
            }
            return application;
        }

        internal static AutoCompletionService GetAutoCompletionService()
        {
            return SPFarm.Local.Services.GetValue<AutoCompletionService>("AutoCompletionService");
        }
    }
}
